﻿using SHRIS.MIS.Common.IdGenerator;
using SHRIS.MIS.Common.Util;
using SHRIS.MIS.Entity.LonginState;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace SHRIS.MIS.Entity
{
    /// <summary>
    /// 数据库实体的基类，所有的数据库实体属性类型都是可空值类型，为了在做条件查询的时候进行判断
    /// 虽然是可空值类型，null的属性值，在底层会根据属性类型赋值默认值，字符串是string.empty，数值是0，日期是1970-01-01
    /// </summary>
    public class BaseEntity
    {
        /// <summary>
        /// 所有表的主键
        /// long返回到前端js的时候，会丢失精度，所以转成字符串
        /// </summary>
        [JsonConverter(typeof(StringJsonConverter))]
        public Guid? Id { get; set; }

        /// <summary>
        /// WebApi没有Cookie和Session，所以需要传入Token来标识用户身份
        /// </summary>
        [NotMapped]
        public string Token { get; set; }

        public virtual void Create()
        {
            this.Id = Guid.NewGuid();
        }
    }

    public class BaseCreateIdEntity : BaseEntity
    {
        /// <summary>
        /// 创建时间
        /// </summary>
        [JsonConverter(typeof(DateTimeJsonConverter))]
        [Description("创建时间")]
        public DateTime? CreateTime { get; set; }

        public new async Task Create()
        {
            base.Create();

            if (this.CreateTime == null)
            {
                this.CreateTime = DateTime.Now;
            }
        }
    }


    public class BaseCreateEntity : BaseEntity
    {
        /// <summary>
        /// 创建时间
        /// </summary>
        [JsonConverter(typeof(DateTimeJsonConverter))]
        [Description("创建时间")]
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// 创建人ID
        /// </summary>
        public Guid? CreatorId { get; set; }

        public new async Task Create()
        {
            base.Create();

            if (this.CreateTime == null)
            {
                this.CreateTime = DateTime.Now;
            }

            if (this.CreatorId == null)
            {
                OperatorInfo user = await Operator.Instance.Current(Token);
                if (user != null)
                {
                    this.CreatorId = user.UserId;
                }
                else
                {
                    if (this.CreatorId == null)
                    {
                        this.CreatorId = Guid.Empty;
                    }
                }
            }
        }
    }

    public class BaseModifyEntity : BaseCreateEntity
    {
        /// <summary>
        /// 数据更新版本，控制并发
        /// </summary>
        public int? Version { get; set; }

        /// <summary>
        /// 修改时间
        /// </summary>
        [JsonConverter(typeof(DateTimeJsonConverter))]
        [Description("修改时间")]
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 修改人ID
        /// </summary>
        public Guid? ModifierId { get; set; }

        public async Task Modify()
        {
            this.Version = 0;
            this.ModifyTime = DateTime.Now;

            if (this.ModifierId == null)
            {
                OperatorInfo user = await Operator.Instance.Current();
                if (user != null)
                {
                    this.ModifierId = user.UserId;
                }
                else
                {
                    if (this.ModifierId == null)
                    {
                        this.ModifierId = Guid.Empty;
                    }
                }
            }
        }
    }

    public class BaseExtensionEntity : BaseModifyEntity
    {
        /// <summary>
        /// 是否删除 1是，0否
        /// </summary>
        [JsonIgnore]
        public int? IsDelete { get; set; }

        public new async Task Create()
        {
            this.IsDelete = 0;
         
            await base.Create();

            await base.Modify();
        }

        public new async Task Modify()
        {
            await base.Modify();
        }
    }

    public class BaseField
    {
        public static string[] BaseFieldList = new string[]
        {
            "Id",
            "IsDelete",
            "CreateTime",
            "ModifyTime",
            "CreatorId",
            "ModifierId",
            "Version"
        };
    }
}
